<template>
  <div class="user-page">
    <div class="top">
      <div class="l">
        <div class="def-btn" v-for="(item,idx) in userBtns"
             @click="userHandle(item,idx)"
             :class="{'on-btn': userIdx == idx}">
          {{ item.name }}
        </div>
      </div>
      <div class="r">
        <div style="display: flex; align-items: center">
          <span style="display: inline-block; width: 100px; color: #366">管理员ID</span>
          <el-input v-model="userId" placeholder="请输入管理员ID"/>
        </div>
        <div class="def-btn" style="margin-left: 10px; margin-right: 30px" @click="searchHandle">搜索</div>
        <div class="def-btn" @click="addHandle">新增</div>
        <div class="def-btn" @click="delHandle">删除</div>
      </div>
    </div>
    <div style="height: calc(100% - 78px)">
      <base-table :pagination="pagination"
                  @currentChange="currentChange">
        <el-table :data="tableData" stripe border
                  :cell-style="{ textAlign: 'center' }"
                  :header-cell-style="{ textAlign: 'center' }"
                  header-row-class-name="headerRow"
                  class="lamp-table"
                  @selection-change="handleSelectionChange"
                  style="width: 100%; height: calc(100% - 20px); overflow: auto">
          <el-table-column type="selection" width="55"/>
          <el-table-column type="index" width="150" label="序号" #default="scope">
            {{ scope.$index < 9 ? '0' + (scope.$index + 1) : scope.$index + 1 }}
          </el-table-column>
          <el-table-column prop="userId" label="管理员ID"/>
          <el-table-column prop="userName" label="用户名"/>
          <el-table-column prop="locked" label="是否锁定" #default="scope">
            <template v-if="scope.row.locked == 1">是</template>
            <template v-else>否</template>
          </el-table-column>
          <el-table-column prop="loginTime" label="最新登录时间"/>
          <el-table-column label="操作" #default="scope">
            <div @click="editHandle(scope.row)">
              <i class="iconfont icon-edit" style="margin-right: 8px"></i>编辑
            </div>
          </el-table-column>
        </el-table>
      </base-table>
    </div>

    <register-user ref="registerRef" @registerOk="init"></register-user>
    <del-user ref="delUser" @delHandle="init"></del-user>
    <edit-user ref="editUser" @editHandle="init"></edit-user>
  </div>

</template>

<script>
import BaseTable from "../../components/baseTable.vue";
import DelUser from "./modules/delUser.vue";
import {getAllUser} from "../../router/api.js";
import RegisterUser from "../../page/login/modules/registerUser.vue";
import EditUser from "../../page/login/modules/editUser.vue";

export default {
  name: "index",
  components: {EditUser, RegisterUser, DelUser, BaseTable},
  data() {
    return {
      userId: '',
      userBtns: [
        {name: '管理员'},
        {name: '灯具控制员'},
        {name: '维护人员'},
        {name: '游客'},
      ],
      userIdx: 0,
      pagination: {
        page: 1,
        size: 18,
        total: 0,
      },
      tableData: [],
      selectUser: []
    }
  },
  created() {
    this.init();
  },
  methods: {
    async init() {
      let res = await getAllUser({
        id: this.userId,
        page: this.pagination.page,
        size: this.pagination.size,
        role: this.userIdx + ''
      });
      if (res.data.code == 200) {
        if (res.data.data) {
          this.tableData = res.data.data.records;
          this.pagination.total = res.data.data.total;
        }
      } else {
        this.tableData = [];
        this.pagination.total = 0;
      }
    },
    currentChange(val) {
      this.pagination.page = val;
      this.init();
    },
    handleSelectionChange(val) {
      this.selectUser = val;
    },
    userHandle(item, idx) {
      this.userIdx = idx;
      this.init();
    },
    searchHandle() {
      this.pagination = {
        page: 1,
        size: 18,
        total: 0,
      }
      this.init();
    },
    addHandle() {
      this.$refs.registerRef.show();
    },
    delHandle() {
      if (!this.selectUser.length) {
        // this.$message.warning('请选择用户');
        return;
      }
      this.$refs.delUser.show(this.selectUser)
    },
    editHandle(row) {
      this.$refs.editUser.edit(row);
    }
  }
}
</script>

<style scoped lang="scss">
.top {
  display: flex;
  justify-content: space-between;
  margin: 0 auto 20px;

  .l {
    display: flex;

    .def-btn {
      width: initial;
      padding: 0 14px;
    }
  }

  .r {
    display: flex;
    align-items: center;
  }
}

.user-page {
  height: calc(100% - 26px);
  //padding-top: 30px;
  position: relative;
  z-index: 20;

  .lamp-table {
    height: calc(100% - 36px);
  }
}
</style>
<style>
.headerRow {
  background: linear-gradient(to right, #6B92F0, #76D0FF) !important;
}

.el-table th.el-table__cell {
  background-color: transparent;
  color: #fff;
}
</style>
